<?php

class Application_Model_Locale
{
    protected $_locale;
    
    public function __construct($locale=null)
    {
        $this->setLocale($locale);
    }
    
    /**
     * Returns an array with valid locales
     *
     * @return array
     */
    static function getValidLocales()
    {
        return Zend_Registry::get('config')->site->locales;
    }
    
    /**
     * Returns default locale
     *
     * @return string
     */
    static function getDefaultLocale()
    {
        return Zend_Registry::get('config')->site->locale;
    }
    
    /**
     * Checks if locale is valid
     *
     * @param string $locale
     * @return bool
     */
    static function isValidLocale($locale)
    {
        if (empty($locale)) return false;
        
        $locales=self::getValidLocales();
        
        return isset($locales->{$locale}) ? true : false;
    }
    
    /**
     * Returns current locale
     *
     * @return string
     */
    public function getLocale()
    {
        return $this->_locale;
    }
    
    /**
     * Returns an array with existing locales
     *
     * @return array
     */
    public function getLocales()
    {
        return self::getValidLocales();
    }
    
    /**
     * Sets current locale
     *
     * @param string $locale
     */
    public function setLocale($locale)
    {
        if (self::isValidLocale($locale))
        {
            $this->_locale=$locale;
        } else 
        {
            $this->_locale=self::getDefaultLocale();
        }
    }
    
    static function load_translations($language)
    {
        $_lang_files=array();
        $translation=array();
        
        $dir=opendir(APPLICATION_PATH.'/languages');
        while (($file=readdir($dir))!==false) 
        {
            if (preg_match('/^(.+)\.'.quotemeta($language).'\.php$/',$file,$matches))
            {
                $_lang_files[]=$matches[1];
            }
        }
        
        if (empty($_lang_files)) return array();
        
        foreach ($_lang_files as $_file)
        {
            if (!file_exists(APPLICATION_PATH.'/languages/'.quotemeta($_file).'.'.$language.'.php')) continue;
            
            include(APPLICATION_PATH.'/languages/'.quotemeta($_file).'.'.$language.'.php');
            
            if (!isset($$_file) || !is_array($$_file)) continue;
            
            $translation=array_merge($$_file,$translation);
        }
        
        return $translation;
    }
    
    static function setTranslation($language=null)
    {
        if (!self::isValidLocale($language))
        {
            $language=self::getDefaultLocale();
        }
        
        $translate = new Zend_Translate(
                                array(
                                    'adapter' => 'array',
                                    'content' => self::load_translations($language),
                                    'locale'  => $language
                                )
                            );
        
        $translate->setLocale($language);
        
        Zend_Registry::set('locale',$translate);
    }
}

